package smartcounters.androidclient;

import java.util.ArrayList;

import smartcounters.androidclient.WebServices.InformationReaderWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.UserInfo;
import smartcounters.androidclient.model.eOperationsEnum;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FindUserActivity extends Activity implements OnClickListener, OnResultReceiver{
	public static ArrayList<UserInfo> s_UsersToShow;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_user_layout);
        
        Button findUserButton = (Button)findViewById(R.id.buttonFindUser);
        findUserButton.setOnClickListener(this);
    }

	public void onClick(View v) {
		EditText findUserUsernameEditText = (EditText)findViewById(R.id.findUserUsernameEditText);
		Button findUserButton = (Button)findViewById(R.id.buttonFindUser);
		
		if (v.getId() == R.id.buttonFindUser){
			findUserUsernameEditText.setEnabled(false);
			findUserButton.setEnabled(false);
			
			TextView messageTextView = (TextView)findViewById(R.id.userFindMessageTextView);
			messageTextView.setText("Please wait...");
			
			InformationReaderWebServiceClient readerClient = new InformationReaderWebServiceClient(Configuration.getInstance().getServerAddress());
			readerClient.RegisterResultReceiver(this);
			readerClient.GetAllUsers(Configuration.getInstance().getUserCredentials());
		}
	}

	public void ReceiveResult(Message i_Msg) {
		EditText findUserUsernameEditText = (EditText)findViewById(R.id.findUserUsernameEditText);
		Button findUserButton = (Button)findViewById(R.id.buttonFindUser);
		
		if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetAllUsers){
			ArrayList<UserInfo> usersToShow = new ArrayList<UserInfo>();
			
			UserInfo[] users = (UserInfo[])i_Msg.obj;
			
			for (UserInfo user : users){
				if ((findUserUsernameEditText.getText().toString().trim().equalsIgnoreCase("")) || (findUserUsernameEditText.getText().toString().trim().equalsIgnoreCase(user.getUsername().trim()))){
					usersToShow.add(user);
				}
			}
			
			if (usersToShow.isEmpty()){
				TextView messageTextView = (TextView)findViewById(R.id.userFindMessageTextView);
				messageTextView.setText("No users found.");
			}
			else{
				TextView messageTextView = (TextView)findViewById(R.id.userFindMessageTextView);
				messageTextView.setText("");
				
				s_UsersToShow = usersToShow;
				
				Intent intent = new Intent();
				intent.setClass(this, FindUserResultsActivity.class);
				startActivity(intent);
			}
			
			findUserUsernameEditText.setEnabled(true);
			findUserButton.setEnabled(true);
		}		
	}
}
